<?php


namespace app\index\controller;


use think\Controller;
use think\Request;

class HuaLeiApi extends Controller
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(!self::$customer_id || !self::$customer_user_id) {
            self::selectAuth();
        }
    }

    protected static $http='http://118.25.228.118:8082';
    protected static $username='TEST';
    protected static $password='123456';
    protected static $customer_id;
    protected static $customer_user_id;

    public static function selectAuth(){
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => self::$http.'/selectAuth.htm',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 15,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array('username' => self::$username,'password' => self::$password),
        ));

        $response = curl_exec($curl);

        $error=curl_error($curl);
        curl_close($curl);
        if($error){
            return json(['code'=>1,'msg'=>'身份认证失败'.$error]);
        }
        $response=str_replace('\'','"',$response);
        $response = json_decode($response, true);
        if($response['ack']==='true'){
            self::$customer_id=$response['customer_id'];
            self::$customer_user_id=$response['customer_userid'];
        }else{
            return json(['code'=>1,'msg'=>'身份认证失败']);
        }
    }

    public function createOrder(){
        $data='{"buyerid":"","consignee_address":"dfgdfgdf","order_piece":"1","consignee_city":"KITTS HILL","consignee_mobile":"22345465677","order_returnsign":"N","consignee_name":"Test Harvey","trade_type":"ZYXT","consignee_postcode":"456456745","consignee_state":"asda","consignee_telephone":"22345465677","consignee_suburb":"asda","country":"US","customer_id":"43522","customer_userid":"41641","orderInvoiceParam":[{"invoice_amount":"90","invoice_pcs":"1","invoice_title":"test6","invoice_weight":"","item_id":"","item_transactionid":"","sku":"测试6","sku_code":"","hs_code":"","transaction_url":"","invoiceunit_code":"","invoice_imgurl":"","invoice_brand":"","invoice_rule":"","invoice_currency":"USD","invoice_taxno":"","origin_country":"","invoice_material":"","invoice_purpose":""}],"order_customerinvoicecode":"2011270001","product_id":"1901","weight":"","product_imagepath":"","order_transactionurl":"","consignee_email":"1924@qq.com","consignee_companyname":"","order_cargoamount":"1365","order_insurance":"","consignee_taxno":"","consignee_doorno":"","cargo_type":"P","order_customnote":""}';
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => self::$http.'/createOrderApi.htm',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array('param'=>$data),
        ));

        $response = curl_exec($curl);

        $error=curl_error($curl);
        curl_close($curl);
        if($error){
            return json(['code'=>1,'msg'=>'创建运单失败'.$error]);
        }
        $response = json_decode($response, true);
        if($response['ack']==='false'){
            $response['message']=urldecode($response['message']);
            $response['return_address']=urldecode($response['return_address']);
            dump($response);
            return;
        }
        dump($response);
    }
}